package com.zuowei.ycy.user.ui.order;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.and.yzy.frame.util.DateTool;
import com.and.yzy.frame.util.RetrofitUtils;
import com.and.yzy.frame.view.dialog.MaterialDialog;
import com.and.yzy.frame.view.linearlistview.LinearListView;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.adapter.OrderGoodsAdapter;
import com.zuowei.ycy.user.domain.OrderDetailsInfo;
import com.zuowei.ycy.user.http.Order;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/28.
 */
public class OrderDetailsAty extends BaseToolbarAty {

    @Bind(R.id.linerly_peisong_info)
    LinearLayout linerlyPeisongInfo;
    @Bind(R.id.tv_pay_type)
    TextView tvPayType;
    @Bind(R.id.linerly_pay_type)
    LinearLayout linerlyPayType;
    private String status[] = {"待支付", "待接单", "配送中", "待收货", "待评价", "已完成", "申请取消订单中", "已取消"};

    @Bind(R.id.listview)
    LinearListView listview;
    @Bind(R.id.tv_delivery_price)
    TextView tvDeliveryPrice;
    @Bind(R.id.v_divier)
    View vDivier;
    @Bind(R.id.tv_buy_number)
    TextView tvBuyNumber;
    @Bind(R.id.tv_all_price)
    TextView tvAllPrice;
    @Bind(R.id.tv_remark)
    TextView tvRemark;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.tv_peisongyuan_name)
    TextView tvPeisongyuanName;
    @Bind(R.id.tv_peisongyuan_phone)
    TextView tvPeisongyuanPhone;
    @Bind(R.id.tv_state)
    TextView tvState;
    @Bind(R.id.tv_order_sn)
    TextView tvOrderSn;
    @Bind(R.id.tv_delivery_time)
    TextView tvDeliveryTime;
    @Bind(R.id.tv_btn_left)
    TextView tvBtnLeft;
    @Bind(R.id.tv_btn_right)
    TextView tvBtnRight;

    private List<OrderDetailsInfo.GoodsListBean> list;
    private OrderGoodsAdapter adapter;

    private OrderDetailsInfo orderDetailsInfo;
    private String order_id;

    @Override
    public int getLayoutId() {
        return R.layout.order_details_aty;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("订单详情");
        order_id = getIntent().getStringExtra("order_id");
        list = new ArrayList<>();
        adapter = new OrderGoodsAdapter(this, list, R.layout.order_details_goods_item);
        listview.setAdapter(adapter);
    }

    @Override
    public boolean setIsInitRequestData() {
        return true;
    }

    private boolean isResume;

    @Override
    protected void onResume() {
        super.onResume();
        if (isResume) {
            showLoadingDialog("");
            doHttp(RetrofitUtils.createApi(Order.class).orderInfo(order_id), 0);
        }
        isResume = true;
    }

    @Override
    public void requestData() {
        showLoadingContentDialog();
        doHttp(RetrofitUtils.createApi(Order.class).orderInfo(order_id), 0);
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what == 0) {
            orderDetailsInfo = AppJsonUtil.getObject(result, OrderDetailsInfo.class);
            showorderData();
            initBtnStatus();
        } else if (what == 2) {//取消订单
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            orderDetailsInfo.setStatus("6");
            showorderData();
            initBtnStatus();
        } else if (what == 3) { //确认收货
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            orderDetailsInfo.setStatus("4");
            showorderData();
            initBtnStatus();
        }
        super.onSuccess(result, call, response, what);
    }

    private void showorderData() {
        list.clear();
        list.addAll(orderDetailsInfo.getGoods_list());
        adapter.notifyDataSetChanged();

        tvDeliveryPrice.setText("¥ " + orderDetailsInfo.getDelivery_price());
        int buynum = 0;
        for (OrderDetailsInfo.GoodsListBean goodsListBean : list) {
            buynum += Integer.valueOf(goodsListBean.getNumber());
        }
        tvBuyNumber.setText("共" + buynum + "份");
        tvAllPrice.setText("¥ " + orderDetailsInfo.getOrder_price());
        tvRemark.setText(orderDetailsInfo.getRemark());
        tvAddress.setText(orderDetailsInfo.getConsignee() + "\t\t" + orderDetailsInfo.getMobile() + "\n" + orderDetailsInfo.getAddress());

        if (TextUtils.isEmpty(orderDetailsInfo.getDelivery_name())) {
            linerlyPeisongInfo.setVisibility(View.GONE);
        } else {
            linerlyPeisongInfo.setVisibility(View.VISIBLE);
            tvPeisongyuanName.setText(orderDetailsInfo.getDelivery_name());
            tvPeisongyuanPhone.setText(orderDetailsInfo.getDelivery_mobile());
        }
        tvOrderSn.setText(orderDetailsInfo.getOrder_sn());
        if (orderDetailsInfo.getDelivery_time().equals("0")) {
            tvDeliveryTime.setText("立即送达");
        } else {
            tvDeliveryTime.setText(DateTool.timestampToStrTime(orderDetailsInfo.getDelivery_time(), ""));
        }
        tvState.setText(status[Integer.parseInt(orderDetailsInfo.getStatus())]);
    }

    private void initBtnStatus() {
        int status = Integer.parseInt(orderDetailsInfo.getStatus());
        if (status == 0) {
            tvBtnLeft.setVisibility(View.VISIBLE);
            tvBtnRight.setVisibility(View.VISIBLE);
            tvBtnRight.setText("立即付款");

            tvBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //去支付
                    Bundle bundle = new Bundle();
                    bundle.putString("order_sn", orderDetailsInfo.getOrder_sn());
                    bundle.putString("order_id", orderDetailsInfo.getOrder_id());
                    bundle.putString("price", orderDetailsInfo.getOrder_price());
                    startActivity(PayOrderAty.class, bundle);
                }
            });

            tvBtnLeft.setText("取消订单");
            tvBtnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //取消订单
                    MaterialDialog dialog = new MaterialDialog(OrderDetailsAty.this);
                    dialog.setMDTitle("提示");
                    dialog.setMDMessage("确定要取消该订单吗?");
                    dialog.setMDConfirmBtnClick(new MaterialDialog.DialogBtnCallBack() {
                        @Override
                        public void dialogBtnOnClick() {
                            //取消订单
                            showLoadingDialog("");
                            doHttp(RetrofitUtils.createApi(Order.class).cancelOrder(orderDetailsInfo.getOrder_id()), 2);
                        }
                    });
                    dialog.show();
                }
            });

        } else if (status == 1) {
            tvBtnLeft.setVisibility(View.GONE);
            tvBtnRight.setVisibility(View.VISIBLE);
            tvBtnRight.setText("取消订单");

            tvBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //取消订单
                    MaterialDialog dialog = new MaterialDialog(OrderDetailsAty.this);
                    dialog.setMDTitle("提示");
                    dialog.setMDMessage("确定要取消该订单吗?");
                    dialog.setMDConfirmBtnClick(new MaterialDialog.DialogBtnCallBack() {
                        @Override
                        public void dialogBtnOnClick() {
                            //取消订单
                            showLoadingDialog("");
                            doHttp(RetrofitUtils.createApi(Order.class).cancelOrder(orderDetailsInfo.getOrder_id()), 2);
                        }
                    });
                    dialog.show();
                }
            });
        } else if (status == 2) { //待配送/配送中
            tvBtnLeft.setVisibility(View.GONE);
            tvBtnRight.setVisibility(View.GONE);
        } else if (status == 3) {
            tvBtnLeft.setVisibility(View.GONE);
            tvBtnRight.setVisibility(View.VISIBLE);
            tvBtnRight.setText("确认收货");

            tvBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //确认收货
                    MaterialDialog dialog = new MaterialDialog(OrderDetailsAty.this);
                    dialog.setMDTitle("提示");
                    dialog.setMDMessage("确定已收到货物，该操作不可撤销?");
                    dialog.setMDConfirmBtnClick(new MaterialDialog.DialogBtnCallBack() {
                        @Override
                        public void dialogBtnOnClick() {
                            //确认收货
                            showLoadingDialog("");
                            doHttp(RetrofitUtils.createApi(Order.class).confirmOrder(orderDetailsInfo.getOrder_id()), 3);
                        }
                    });
                    dialog.show();
                }
            });
        } else if (status == 4) {
            tvBtnLeft.setVisibility(View.GONE);
            tvBtnRight.setVisibility(View.VISIBLE);
            tvBtnRight.setText("立即评价");
            tvBtnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //评价
                    Bundle bundle = new Bundle();
                    bundle.putString("order_id", order_id);
                    startActivity(OrderPinjiaAty.class, bundle);
                }
            });
        } else if (status == 5) {
            tvBtnLeft.setVisibility(View.GONE);
            tvBtnRight.setVisibility(View.GONE);
        } else if (status == 6) {
            tvBtnLeft.setVisibility(View.GONE);
            tvBtnRight.setVisibility(View.GONE);
        } else if (status == 7) {
            tvBtnLeft.setVisibility(View.GONE);
            tvBtnRight.setVisibility(View.GONE);
        }


    }
}
